package com.xhj.xmly.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.xhj.xmly.R;
import com.xhj.xmly.callback.BroadcastAdapterListener;

import java.util.List;

/**
 * Created by Administrator on 2016/12/6.
 */

public class TextAdapter extends RecyclerView.Adapter<TextAdapter.ViewHolder> implements View.OnClickListener {
    private Context context;
    private List<String> list;
    private BroadcastAdapterListener listener;
    private int count = 0;

    public TextAdapter(Context context, List<String> list,BroadcastAdapterListener listener) {
        this.context = context;
        this.list = list;
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.broadcast_recycler_text,parent,false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        if(position ==list.size()-1){
            count++;
            if(count%2 == 1){
                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_triangle_down);
                holder.imageView.setImageBitmap(bitmap);
            }else {
                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_triangle_up);
                holder.imageView.setImageBitmap(bitmap);
            }
            holder.textView.setOnClickListener(this);
        }
        holder.textView.setText(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        listener.setCount();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView textView;
        private ImageView imageView;
        public ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.broadcast_text);
            imageView = (ImageView) itemView.findViewById(R.id.broadcast_text_image);
        }
    }
}
